package com.fanvil.videoconference;

import android.annotation.SuppressLint;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.MotionEvent;
import android.widget.Toast;
import java.util.Locale;
import java.util.TimeZone;
import com.fanvil.videoconference.FvlVideoConsumer;
import com.fanvil.videoconference.FvlSurfaceHolder;
import com.fanvil.videoconference.FvlLogger;
/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class FullscreenActivity extends AppCompatActivity {
    private static FvlLogger sLogger = FvlLogger.getLogger(FullscreenActivity.class.getSimpleName(), FvlLogger.DEBUG);
    float x1 = 0;
    float x2 = 0;
    float y1 = 0;
    float y2 = 0;
    private VideoSurfaceView mSurfaceBelow;
    private VideoSurfaceView mSurfaceAbove;
    private VideoSurfaceView mSurfaceThird;
    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE" };

    private static final String[] availableTimeZoneIds = TimeZone.getAvailableIDs();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_fullscreen);

        VideoManager.getInstance();

        hideSystemUi(getWindow());
        mSurfaceAbove = (VideoSurfaceView) findViewById(R.id.surface_above);
        mSurfaceAbove.setRole(FvlSurfaceHolder.Role.LOCAL);
        mSurfaceAbove.setVisibility(View.VISIBLE );
        mSurfaceBelow = (VideoSurfaceView) findViewById(R.id.surface_below);
        mSurfaceBelow.setRole(FvlSurfaceHolder.Role.REMOTE);
        mSurfaceBelow.setVisibility(View.VISIBLE );
        //mSurfaceBelow.setZOrderMediaOverlay(true);
        //mSurfaceBelow.setZOrderOnTop(true);
        //mSurfaceBelow.getHolder().setFormat(PixelFormat.TRANSLUCENT);
        /*
        mSurfaceAbove = (VideoSurfaceView) findViewById(R.id.surface_above);
        mSurfaceAbove.setRole(FvlSurfaceHolder.Role.LOCAL);
        mSurfaceAbove.setVisibility(View.VISIBLE );
        */
        mSurfaceThird = (VideoSurfaceView) findViewById(R.id.surface_third);
        mSurfaceThird.setRole(FvlSurfaceHolder.Role.REMOTE2);
        mSurfaceThird.setVisibility(View.VISIBLE );

        if (!checkPermissionAllGranted(PERMISSIONS_STORAGE)) {
            requestPermissions(PERMISSIONS_STORAGE, 10001);
        }
    }
    private void hideSystemUi(Window window) {

        window.getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        |View.SYSTEM_UI_FLAG_IMMERSIVE
                        |View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        |View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        |View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                        |View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                        |View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
    }
    private boolean checkPermissionAllGranted(String[] permissions) {
        for (String permission : permissions) {
            if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
                // 只要有一个权限没有被授予, 则直接返回 false
                return false;
            }
        }
        return true;
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action=event.getAction();
        int  x=(int) event.getX();
        int y=(int) event.getY();
        sLogger.d("onTouchEvent: action=" + action + " x=" + x + " y=" + y);
        //继承了Activity的onTouchEvent方法，直接监听点击事件
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            //当手指按下的时候
            x1 = event.getX();
            y1 = event.getY();
        }
        if(event.getAction() == MotionEvent.ACTION_UP) {
            //当手指离开的时候
            x2 = event.getX();
            y2 = event.getY();
            if(y1 - y2 > 50) {
                Toast.makeText(FullscreenActivity.this, "向上滑", Toast.LENGTH_SHORT).show();
            } else if(y2 - y1 > 50) {
                Toast.makeText(FullscreenActivity.this, "向下滑", Toast.LENGTH_SHORT).show();
            } else if(x1 - x2 > 50) {
                Toast.makeText(FullscreenActivity.this, "向左滑", Toast.LENGTH_SHORT).show();
            } else if(x2 - x1 > 50) {
                Toast.makeText(FullscreenActivity.this, "向右滑", Toast.LENGTH_SHORT).show();
            }
        }
        if (x<1000 && x > 750 && y < 200 && y > 40) {
            //右上小窗口 index=1
            Toast.makeText(FullscreenActivity.this, "右上小窗口", Toast.LENGTH_SHORT).show();
            //MultiVideoManager.getInstance().setViewMode(1);
            //MultiVideoManager.getInstance().setViewMode(0,2);
            //MultiVideoManager.getInstance().setViewMode(1,1);
            //MultiVideoManager.getInstance().setViewMode(2,0);
        }else if (x<1000 && x > 750 && y < 400 && y > 250) {
            //右下小窗口 index=2
            Toast.makeText(FullscreenActivity.this, "右下小窗口", Toast.LENGTH_SHORT).show();
            //MultiVideoManager.getInstance().setViewMode(2);
            //MultiVideoManager.getInstance().setViewMode(0,1);
            //MultiVideoManager.getInstance().setViewMode(1,0);
            //MultiVideoManager.getInstance().setViewMode(2,2);
        }else if (x<600 && x > 10 && y < 500 && y > 40) {
            //左大窗口 index=0
            Toast.makeText(FullscreenActivity.this, "左大窗口", Toast.LENGTH_SHORT).show();
            //MultiVideoManager.getInstance().setViewMode(0);
            //MultiVideoManager.getInstance().setViewMode(0,1);
            //MultiVideoManager.getInstance().setViewMode(1,0);
            //MultiVideoManager.getInstance().setViewMode(2,2);
        }
        return super.onTouchEvent(event);
    }
}
